#include "PluginInterface.h"
#include "PacketPriority.h"
#include "RakPeerInterface.h"
#include <string>
#include <map>
#include "ChatOrder.h"

using namespace std;

namespace ChatApp
{
struct Items
{
	bool isequip;
	std::string name;

	Items()
		: isequip(false)
		, name("")
	{
	}
};

struct GamePC
{
	int lv;
	int exp;
    int hp;
    int str;
    int def;
    int hit;
    int dex;
    int _int;
    int mdf;
    int gold;
    int carry;
    int maxcarry;
    std::map<unsigned int, Items> pack;

    GamePC()
        : lv(0)
		, exp(0)
        , hp(0)
        , str(0)
        , def(0)
        , hit(0)
        , dex(0)
        , _int(0)
        , mdf(0)
        , gold(0)
        , carry(0)
        , maxcarry(0)
    {
    }
};
// Struct of incoming member
struct ClientMember
{
	SystemAddress sa;             // System address of member
	int friendId[MAX_FRIEND_NUM]; // Friend list of member
	bool isConnected;             // Status of member
    std::string name;             // Client name
    GamePC pc;

    ClientMember()
        : isConnected(false)
    {
        name = "player";
    }
}; // End of SystemAddressMember

class ChatServerPlugin : public PluginInterface
{
public:
	ChatServerPlugin();
	~ChatServerPlugin();
	
	OrderMessage CreateOrder( Packet *packet );
	bool InsertClient( SystemAddress address );
	void DeleteClient( int systemIndex );
	unsigned char GetPacketId( Packet *p );

	ClientMember clientMember[MAX_INCOMING_CONNECTIONS];

protected:
	PluginReceiveResult OnReceive( RakPeerInterface *peer, Packet *packet );
	void OnCloseConnection( RakPeerInterface *peer, SystemAddress systemAddress );
	void OnShutdown( RakPeerInterface *peer );
	DefaultChatOrder OnMessage ( Packet *packet );

private:
	void InitClientMember( int memberID );
	bool CheckFriendList( char *message, int memberAsk, int memberAnswer );
	bool CheckFriend( int member1, int member2, bool unilateral );
	void AddFriend ( int member1, int member2 );
};
}